using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;

namespace SelfMadeMillionaire.DAL.Migrations
{
	public sealed class Configuration : DbMigrationsConfiguration<DataContext>
	{
		public Configuration()
		{
			AutomaticMigrationsEnabled = true;
		}


		protected override void Seed(DataContext context)
		{
			//  This method will be called after migrating to the latest version.

			//  You can use the DbSet<T>.AddOrUpdate() helper extension method 
			//  to avoid creating duplicate seed data. E.g.
			//
			//    context.People.AddOrUpdate(
			//      p => p.FullName,
			//      new Person { FullName = "Andrew Peters" },
			//      new Person { FullName = "Brice Lambson" },
			//      new Person { FullName = "Rowan Miller" }
			//    );
			//

			var types = new List<IssueType>
			{
				new IssueType {Title = "Trade Alert", Status = 1},
				new IssueType {Title = "Portfolio Update", Status = 1},
				new IssueType {Title = "Stock Analysis", Status = 1},
				new IssueType {Title = "Miscellaneous", Status = 1},
				new IssueType {Title = "Year-End Update", Status = 1},
				new IssueType {Title = "Portfolio Performance", Status = 1},
                new IssueType {Title = "Order Alert", Status = 1}
			};
			types.ForEach(s => context.IssueTypes.AddOrUpdate(p => p.Title, s));
			context.SaveChanges();


			if (!context.Schedulers.Any())
			{
				var issuetype = types.Single(s => s.Title == "Trade Alert").ID;
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Free,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 2
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Silver,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 1
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Gold,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 0
				});

				issuetype = types.Single(s => s.Title == "Portfolio Update").ID;
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Free,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 2
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Silver,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 1
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Gold,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 0
				});

				issuetype = types.Single(s => s.Title == "Stock Analysis").ID;
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Free,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 14
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Silver,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 7
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Gold,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 0
				});

				issuetype = types.Single(s => s.Title == "Year-End Update").ID;
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Free,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 0
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Silver,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 0
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = issuetype,
					AccessLevel = (byte) SubscriptionType.Gold,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 0
				});

				issuetype = types.Single(s => s.Title == "Portfolio Performance").ID;
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = 8,
					AccessLevel = (byte) SubscriptionType.Free,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 0
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = 8,
					AccessLevel = (byte) SubscriptionType.Silver,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 0
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					TypeId = 8,
					AccessLevel = (byte) SubscriptionType.Gold,
					NotificationType = (byte) NotificationType.Newsletter,
					OffsetInDays = 0
				});


				context.Schedulers.AddOrUpdate(new Scheduler
				{
					AccessLevel = (byte) SubscriptionType.Free,
					NotificationType = (byte) NotificationType.Publication,
					OffsetInDays = 14
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					AccessLevel = (byte) SubscriptionType.Silver,
					NotificationType = (byte) NotificationType.Publication,
					OffsetInDays = 7
				});
				context.Schedulers.AddOrUpdate(new Scheduler
				{
					AccessLevel = (byte) SubscriptionType.Gold,
					NotificationType = (byte) NotificationType.Publication,
					OffsetInDays = 0
				});
			}

			if (!context.Publications.Any()) 
			{
				context.Publications.AddOrUpdate(new Publication
				{
					Title = "Test 1",
					ShortText = "Short 1",
					HtmlContent = "Html Content",
					ProductType = (byte)ProductType.SelfMadeMillionaire,
					MinAccessLevel = (byte)SubscriptionType.Free,
					IsShowOnHomePage = true,
					Date = DateTime.Now,
					CreatedDate = DateTime.Now,
					Status = 1
				});

				context.Publications.AddOrUpdate(new Publication
				{
					Title = "Test 2",
					ShortText = "Short 2",
					HtmlContent = "Html Content 2",
					ProductType = (byte)ProductType.SelfMadeMillionaire,
					MinAccessLevel = (byte)SubscriptionType.Free,
					IsShowOnHomePage = true,
					Date = DateTime.Now,
					CreatedDate = DateTime.Now,
					Status = 1
				});

				context.Publications.AddOrUpdate(new Publication
				{
					Title = "Test 3",
					ShortText = "Short 3",
					HtmlContent = "Html Content 3",
					ProductType = (byte)ProductType.SelfMadeMillionaire,
					MinAccessLevel = (byte)SubscriptionType.Free,
					IsShowOnHomePage = true,
					Date = DateTime.Now,
					CreatedDate = DateTime.Now,
					Status = 1
				});

				context.Publications.AddOrUpdate(new Publication
				{
					Title = "Test 4",
					ShortText = "Short 4",
					HtmlContent = "Html Content 4",
					ProductType = (byte)ProductType.SelfMadeMillionaire,
					MinAccessLevel = (byte)SubscriptionType.Free,
					IsShowOnHomePage = true,
					Date = DateTime.Now,
					CreatedDate = DateTime.Now,
					Status = 1
				});


				context.Publications.AddOrUpdate(new Publication
				{
					Title = "Test 1",
					ShortText = "Short 1",
					HtmlContent = "Html Content",
					ProductType = (byte)ProductType.MaggiesMoneyMountain,
					MinAccessLevel = (byte)SubscriptionType.Free,
					IsShowOnHomePage = true,
					Date = DateTime.Now,
					CreatedDate = DateTime.Now,
					Status = 1
				});

				context.Publications.AddOrUpdate(new Publication
				{
					Title = "Test 2",
					ShortText = "Short 2",
					HtmlContent = "Html Content 2",
					ProductType = (byte)ProductType.MaggiesMoneyMountain,
					MinAccessLevel = (byte)SubscriptionType.Free,
					IsShowOnHomePage = true,
					Date = DateTime.Now,
					CreatedDate = DateTime.Now,
					Status = 1
				});

				context.Publications.AddOrUpdate(new Publication
				{
					Title = "Test 3",
					ShortText = "Short 3",
					HtmlContent = "Html Content 3",
					ProductType = (byte)ProductType.MaggiesMoneyMountain,
					MinAccessLevel = (byte)SubscriptionType.Free,
					IsShowOnHomePage = true,
					Date = DateTime.Now,
					CreatedDate = DateTime.Now,
					Status = 1
				});

				context.Publications.AddOrUpdate(new Publication
				{
					Title = "Test 4",
					ShortText = "Short 4",
					HtmlContent = "Html Content 4",
					ProductType = (byte)ProductType.MaggiesMoneyMountain,
					MinAccessLevel = (byte)SubscriptionType.Free,
					IsShowOnHomePage = true,
					Date = DateTime.Now,
					CreatedDate = DateTime.Now,
					Status = 1
				});
		}

	if (!context.TrackingPortfolio.Any())
				context.TrackingPortfolio.AddOrUpdate(new TrackingPortfolioInfo
				{
					FromDate = new DateTime(2012, 7, 20),
					ToDate = new DateTime(2013, 9, 13),
					TrackingPortfolio = 23.53,
					DowIndustrials = 18.93,
					SP500 = 24.72,
					Nasdaq = 28.04
				});

			context.SaveChanges();
		}
	}
}